package com.factory.iamp.controller;

import com.factory.iamp.pojo.Role;
import com.factory.iamp.pojo.RoleBuild;
import com.factory.iamp.service.RoleService;
import com.factory.iamp.utils.helper.PageHelper;
import com.factory.iamp.utils.token.LoginToken;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/role")
@Validated
public class RoleController {
    @Resource
    private RoleService roleService;

    @GetMapping("/list/{limit}/{page}")
    @LoginToken
    public PageHelper getAllRole(@PathVariable("limit") int limit, @PathVariable("page") int page) {
        PageHelper result = new PageHelper(roleService.Count(), page, limit);
        result.setData(roleService.ListPaged(result));
        return result;
    }

    @PostMapping("/add")
    @LoginToken(checkPrivate = true)
    public Role addRole(@RequestBody @Valid Role role) throws Throwable {
        return roleService.addRole(role);
    }

    @PostMapping("/set")
    @LoginToken(checkPrivate = true)
    public void buildRoleUser(@RequestBody @Valid RoleBuild roleBuild) {
        roleService.buildRoleUser(roleBuild);
    }

    @DeleteMapping("/{rid}")
    @LoginToken(checkPrivate = true)
    public void deleteRole(@PathVariable("rid") int rid) {
        roleService.deleteRole(rid);
    }
}
